import numpy as np
from keras.engine.saving import load_model
from PIL import Image
from photoClassify.project.charClassify.captchaDataLoad import single_num_cha_path, four_num_cha_path, single_num_path

# 识别
while True:
    type = input('输入要识别的图片类型(1为1位数字,2为1位数字或字母,3为4位数字或字母):')
    if type == '1':
        model = load_model('saved_models/1_num.h5')
        path = single_num_path
    elif type == '2':
        model = load_model('saved_models/1_num_cha.h5')
        path = single_num_cha_path
    elif type == '3':
        model = load_model('saved_models/4_num_cha.h5')
        path = four_num_cha_path
    else:
        continue
    break
while True:
    picName = input("输入要识别的图片文件名: ")
    try:
        x_valid = np.asarray([np.asarray(Image.open(path + picName + '.png'))])
    except:
        print('不存在的png图片\n')
        continue
    x_valid = x_valid.astype('float32') / 255
    proba = model.predict(x_valid)
    if proba.shape[-1] > 1:
        result = proba.argmax(axis=-1)
    else:
        result = (proba > 0.5).astype('int32')

    # 转化为字母
    if (9 < int(result[0]) < 37):
        result = [chr(int(result[0])+87)]
    print('---预测结果:', result, '\n')
    continue
